using UnityEngine;
using System.Collections;

public class Predator : MonoBehaviour
{
	public GameObject crosshair;	
	IEnumerator Start ()
	{
		yield return new WaitForSeconds(Random.Range(30f, 60f));
		if(Controller.spawnedCar != null)
		{
			Instantiate(crosshair, Controller.spawnedCar.transform.position, Quaternion.identity);
			yield return new WaitForSeconds(5);
			if(Controller.spawnedCar != null)
				StartCoroutine(Attack());
        }
	}
	
	IEnumerator Attack ()
	{
		while (true)
		{
			transform.position = Vector3.MoveTowards(transform.position, Controller.spawnedCar.transform.position, 1f);
			yield return null;	
		}
	}
	
	void OnTriggerEnter (Collider col)
	{
		if(col.gameObject.tag == "Car")
		{
			col.gameObject.SendMessage("Die", SendMessageOptions.DontRequireReceiver);
			Destroy(gameObject);
		}
	}
}
